#include "mainwidget.h"

MainWidget* MainWidget::_instance = nullptr;

MainWidget* MainWidget::getInstance()
{
    if(_instance == nullptr)
    {
        _instance = new MainWidget();

    }
    return _instance;
}

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{

    this->setWindowTitle("myChat");
    this->setWindowIcon(QIcon(":/resource/Image/windowIcon.png"));
    this->setMinimumHeight(500);
    initWindow();
    initWindowLeft();
    initWindowMid();
    initWindowRight();
    initSignalSlots();

}

MainWidget::~MainWidget() {}

void MainWidget::initWindow()
{
    QHBoxLayout *hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0,0,0,0);


    windowLeft = new QWidget;
    windowMid = new QWidget;
    windowRight = new QWidget;

    windowLeft->setFixedWidth(65);
    windowMid->setFixedWidth(250);
    windowRight->setMinimumWidth(550);

    windowLeft->setStyleSheet("QWidget { background-color: rgb(46, 46, 46); }");
    windowMid->setStyleSheet("QWidget{background-color: rgb(247,247,247); border-right:1px solid rgb(230,230,230);}");
    windowRight->setStyleSheet("QWidget{background-color: rgb(247,247,247);}");



    hlayout->addWidget(windowLeft);
    hlayout->addWidget(windowMid);
    hlayout->addWidget(windowRight);



    this->setLayout(hlayout);
}

void MainWidget::initWindowLeft()
{
    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->setSpacing(20);
    vLayout->setContentsMargins(0, 20, 0, 0);


    userAvatarBtn = new QPushButton;
    userAvatarBtn->setFixedSize(35,30);
    userAvatarBtn->setIcon(QIcon(":/resource/Image/avatar_1.png"));
    userAvatarBtn->setIconSize(QSize(35,30));
    userAvatarBtn->setStyleSheet("QPushButton { background-color: transparent; }");
    vLayout->addWidget(userAvatarBtn, 1, Qt::AlignTop | Qt::AlignHCenter);


    sessionTabBtn = new QPushButton;
    sessionTabBtn->setFixedSize(35,30);
    sessionTabBtn->setIcon(QIcon(":/resource/Image/session_active.png"));
    sessionTabBtn->setIconSize(QSize(35,30));
    sessionTabBtn->setStyleSheet("QPushButton { background-color: transparent; }");
    vLayout->addWidget(sessionTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    friendTabBtn = new QPushButton;
    friendTabBtn->setFixedSize(35,30);
    friendTabBtn->setIcon(QIcon(":/resource/Image/friend_inactive.png"));
    friendTabBtn->setIconSize(QSize(35,30));
    friendTabBtn->setStyleSheet("QPushButton { background-color: transparent; }");
    vLayout->addWidget(friendTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    applyTabBtn = new QPushButton;
    applyTabBtn->setFixedSize(35,30);
    applyTabBtn->setIcon(QIcon(":/resource/Image/apply_inactive.png"));
    applyTabBtn->setIconSize(QSize(35,30));
    applyTabBtn->setStyleSheet("QPushButton { background-color: transparent; }");
    vLayout->addWidget(applyTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    vLayout->addStretch(20);
    windowLeft->setLayout(vLayout);
}

void MainWidget::initSignalSlots()
{
    connect(sessionTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToSession);
    connect(friendTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToFriend);
    connect(applyTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToApply);
}

void MainWidget::switchTabToSession()
{
    activeTab = SESSION_LIST;
    sessionTabBtn->setIcon(QIcon(":/resource/Image/session_active.png"));
    friendTabBtn->setIcon(QIcon(":/resource/Image/friend_inactive.png"));
    applyTabBtn->setIcon(QIcon(":/resource/Image/apply_inactive.png"));

    loadSessionList();
}

void MainWidget::switchTabToFriend()
{
    activeTab = FRIEND_LIST;
    sessionTabBtn->setIcon(QIcon(":/resource/Image/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/resource/Image/friend_active.png"));
    applyTabBtn->setIcon(QIcon(":/resource/Image/apply_inactive.png"));

    loadFriendList();
}

void MainWidget::switchTabToApply()
{
    activeTab = APPLY_LIST;
    sessionTabBtn->setIcon(QIcon(":/resource/Image/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/resource/Image/friend_inactive.png"));
    applyTabBtn->setIcon(QIcon(":/resource/Image/apply_active.png"));

    loadApplyList();
}

void MainWidget::initWindowMid()
{

    QGridLayout *gLayout = new QGridLayout;
    gLayout->setSpacing(5);
    searchEdit = new QLineEdit;
    searchEdit->setFixedHeight(20);

    searchEdit->setPlaceholderText("搜索");
    searchEdit->setStyleSheet("QLineEdit{background-color: rgb(240,240,240); border: none;}");

    addFriendBtn = new QPushButton;
    addFriendBtn->setFixedSize(25, 25);
    addFriendBtn->setIcon(QIcon(":/resource/Image/cross.png"));
    QString style = "QPushButton { border-radius: 5px; background-color: rgb(226, 226, 226); }";
    style += " QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    addFriendBtn->setStyleSheet(style);

    SessionFriendArea *sessionFriendArea = new SessionFriendArea;
    gLayout->addWidget(searchEdit, 0, 0);
    gLayout->addWidget(addFriendBtn, 0, 1);
    gLayout->addWidget(sessionFriendArea, 1, 0, 1, 2);

    windowMid->setLayout(gLayout);
}
void MainWidget::initWindowRight()
{
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setSpacing(0);
    vLayout->setContentsMargins(0,0,0,0);
    windowRight->setLayout(vLayout);

    // 标题栏区域
    QWidget* titleWidget = new QWidget;
    titleWidget->setStyleSheet("border-bottom: 1px solid rgb(230,230,230);}");
    titleWidget->setFixedHeight(42);
    vLayout->addWidget(titleWidget);

    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setSpacing(0);
    hLayout->setContentsMargins(10,0,10,0);

    sessionLabel = new QLabel();
    sessionLabel->setStyleSheet("QLabel{font-size: 23px;");

#ifdef TEST_UI
    sessionLabel->setText("会话标题(测试)");
#endif
    hLayout->addWidget(sessionLabel);

    extraBtn = new QPushButton();
    extraBtn->setFixedSize(30, 30);
    extraBtn->setIconSize(QSize(30,30));
    extraBtn->setIcon(QIcon(":/resource/Image/more.png"));
    extraBtn->setStyleSheet("QPushButton{border:none; background-color: rgb(245,245,245);} QPushButton:hover{background-color: rgb(220, 220, 220);}");
    hLayout->addWidget(extraBtn);
    titleWidget->setLayout(hLayout);

    // 消息展示区
    messageShowArea = new MessageShowArea();
    vLayout->addWidget(messageShowArea);

    messageEditArea = new MessageEditArea();
    vLayout->addWidget(messageEditArea);
}

void MainWidget::loadSessionList()
{

}
void MainWidget::loadFriendList()
{

}
void MainWidget::loadApplyList()
{

}
